package org.example.springoauth2.securitycommon.config.authenticate;

import org.example.springoauth2.securitycommon.entity.SysUser;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hzq
 * @date 2021/10/11 11:51
 */
public class EnhanceTokenEnhancer implements TokenEnhancer {

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        // 自定token增强
        final Map<String, Object> additionalInfo = new HashMap<>(8);
        SysUser sysUser = (SysUser) authentication.getUserAuthentication().getPrincipal();
        additionalInfo.put("jdbcUser_id", sysUser.getJdbcUser().getId());
        additionalInfo.put("jdbcUser_nickname", sysUser.getJdbcUser().getNickname());
        additionalInfo.put("jdbcUser_tenantId", sysUser.getJdbcUser().getTenantId());
        additionalInfo.put("jdbcUser_unionId", sysUser.getJdbcUser().getUnionId());
        additionalInfo.put("jdbcUser_userType", sysUser.getJdbcUser().getUserType());
        additionalInfo.put("jdbcUser_gender", sysUser.getJdbcUser().getGender());
        additionalInfo.put("jdbcUser_jdbcUserAuth_authType", sysUser.getJdbcUser().getJdbcUserAuth().getAuthType());
        additionalInfo.put("jdbcUser_jdbcUserAuth_key", sysUser.getJdbcUser().getJdbcUserAuth().getKey());
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
        return accessToken;
    }
}
